package org.grow.jssp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.jssp.dao.DownloadLogMapper;
import org.grow.jssp.entity.DownloadLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 2022/3/10
 */

@Controller
public class AuthController {
    @GetMapping("")
    public ModelAndView doGet(HttpServletRequest request ,
                              HttpServletResponse response){

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");

        return modelAndView;
    }

    @PostMapping("/auth")
    public ModelAndView auth(HttpServletRequest request ,
                             HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username "+username+"尝试登陆");

        request.getSession().setAttribute("principle",username);

        ModelAndView dashboard = new ModelAndView("redirect:dashboard");
        return dashboard;
//
    }

    @Autowired
    private DownloadLogMapper downloadLogMapper;
    @GetMapping("/dashboard")
    public ModelAndView dashboard(HttpServletRequest request ,
                                  HttpServletResponse response){
//        List<DownloadLog> results = downloadLogMapper.query();

        String filename = null;
        IPage<DownloadLog> page = new Page<>(1,5);
        IPage<DownloadLog> pageResults = downloadLogMapper.dynamicQuery(page, filename);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("dashboard");
        modelAndView.addObject("results",pageResults.getRecords());
        modelAndView.addObject("total",pageResults.getTotal());
        modelAndView.addObject("pages",pageResults.getPages());
        return modelAndView;
    }
//    action="/dashboard" method="post"
    @PostMapping("/dashboard")
    public ModelAndView dashboardPost(HttpServletRequest request ,
                                      HttpServletResponse response){
        String filename = request.getParameter("filename");
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        System.out.println(filename);
        System.out.println(pageNum);
        System.out.println(pageSize);
//        List<DownloadLog> downloadLogs = downloadLogMapper.dynamicQuery(filename);
//        System.out.println(downloadLogs);
        IPage<DownloadLog> page = new Page<>(pageNum,pageSize);
        IPage<DownloadLog> pageResults = downloadLogMapper.dynamicQuery(page, filename);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("dashboard");
        modelAndView.addObject("results",pageResults.getRecords());
        modelAndView.addObject("total",pageResults.getTotal());
        modelAndView.addObject("pages",pageResults.getPages());
//        modelAndView.addObject()
        return modelAndView;
    }
}
